import msg from "../../utils/msg";
import { activity_list } from "../../utils/request/api";
import { filePath1 } from "../../utils/request/route";
import User from "../../utils/user";

Component({
  data: {
    list: [],
    searchList:[] ,
    showList: [] ,
    windowHeight: 0,
    inputSearch:'',
    tabSearch:'',
    filePath1:filePath1,
  },
  methods: {
    goToDetail(e:any){
      let index = e.currentTarget.dataset.index;
      let data:any = this.data.showList[index];
      wx.navigateTo({
        url:"/pages/act/actDetail/actDetail"+'?id='+data.id,
      });
    },
    getInputSearch(e: any) {
      let input = e.detail;
      this.setData({
        inputSearch: input,
      });
      this.search();
    },
    launchAct:async function() {
      let user = new User();
      if(!user.checkLogin(1)) return;
      if(!user.checkMerchant()){
        await msg.confirm('您暂未登录商户账号','是否跳转登录/注册?');
        wx.navigateTo({url:"/pages/user/merchant/merchant"});
        return;
      }
      wx.navigateTo({
        url: "/pages/act/launchAct/launchAct",
      })
    },
    onShow:async function() {
      let user = new User;
      msg.Loading();
      let res_act: any = await activity_list({ GF_ID: user.GF_ID, type: 1 });
      msg.hideLoading();
      this.setData({
        list: res_act.data.datas,
        searchList:JSON.parse(JSON.stringify(res_act.data.datas)),
        showList: JSON.parse(JSON.stringify(res_act.data.datas)),
      });
    },
    //tab筛选
    onChange(e:any){
      let index = e.detail.index;
      let tabSearch="";
      switch(index){
        case 1: tabSearch='报名中';break;
        case 2: tabSearch='活动中';break;
        case 3: tabSearch='已结束';break;
      }
      this.setData({
        tabSearch,
      });
      this.search();
    },
    tabSearch(){
      let searchList:any = this.data.searchList;
      let tabSearch = this.data.tabSearch;
      if(!tabSearch) return;
      let data = this.data.searchList;
      searchList=[];
      data.forEach((item:any)=>{
        if(item.state_name==tabSearch)
          searchList.push(item);
      });
      this.setData({
        searchList:searchList,
      });
    },
    search(){
      let list=this.data.list;
      this.setData({
        searchList:list,
      });
      //各类搜索
      this.inputSearch();
      this.tabSearch();
      //重置显示数据
      this.setData({showList:[]});
      this._pushShowData();
    },
    //渲染新数据
    _pushShowData() {
      let data = this.data.searchList;
      let showData = this.data.showList;
      if (data.length <= showData.length) return;
      let start = showData.length;
      for (let i = start; (i < start + 20 && i < data.length); i++)
        showData.push(data[i]);
      this.setData({
        showList: showData,
      });
    },
    inputSearch() {
      let searchList:any = this.data.searchList;
      let input = this.data.inputSearch;
      if(input){
        let data = searchList;
        searchList=[];
        data.forEach((item:any)=>{
          if(item.name.includes(input)||item.address.includes(input))
            searchList.push(item);
        });
      }
      this.setData({
        searchList:searchList,
      });
    },
    onLoad: async function () {
      this.setTopHeight();
      
    },
    setTopHeight() {
      let windowHeight = wx.getSystemInfoSync().windowHeight;
      this.setData({
        windowHeight: windowHeight,
      })
    },
  },
})
